﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Memory
{
    public class TButton : Button
    {
        private bool active = false;
        private bool isLocked = false;
        private bool isOpen = false;
        private Point position = Point.Empty;
        private int index = -1;
        private System.Drawing.SolidBrush ActiveBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(50, 128, 255, 128));
        private MemoryForm form = null;

        public bool Active
        {
            get
            {
                return this.active;
            }
            set
            {
                if (value != this.active)
                {
                    this.active = value;
                    this.Refresh();
                }
            }
        }

        public bool IsLocked
        {
            get
            {
                return this.isLocked;
            }
        }

        public bool IsOpen
        {
            get
            {
                return this.isOpen;
            }
        }

        public Point Position
        {
            get
            {
                return this.position;
            }
            set
            {
                this.position = value;
            }
        }

        public int Index
        {
            get
            {
                return this.index;
            }
        }

        public TButton(Point position, MemoryForm form, int index)
        {
            this.position = position;
            this.form = form;
            this.index = index;   
            this.Image = this.form.Images[MemoryForm.BackgroundIndex];
            this.ImageAlign = ContentAlignment.MiddleCenter;
            this.Click += new EventHandler(TButton_Click);
        }

        public void TButton_Click(object sender, EventArgs e)
        {
            this.form.Open(this);
            this.isOpen = true;
            this.Image = this.form.Images[this.index];
        }
        
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);

            if (this.active) 
            {
                pevent.Graphics.FillRectangle(this.ActiveBrush, pevent.ClipRectangle);
            }
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.active = true;
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.active = false;
        }

        public void Close()
        {
            if (!this.isLocked)
            {
                this.isOpen = false;
                this.Image = this.form.Images[MemoryForm.BackgroundIndex];
            }
        }

        public void Lock()
        {
            this.isLocked = true;
        }
    }
}
